package cn.lystudio.rohmtimeopwide_background_v11.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.lystudio.rohmtimeopwide_background_v11.entity.dto.MachineDTO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.dto.PartDTO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.MachinePO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.PartPO;
import cn.lystudio.rohmtimeopwide_background_v11.mapper.PartPOMapper;
import cn.lystudio.rohmtimeopwide_background_v11.service.MachineService;
import cn.lystudio.rohmtimeopwide_background_v11.service.PartService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;

@Service
@Slf4j
@DS("master")

public class PartServiceImpl implements PartService {
    @Resource
    private PartPOMapper partPOMapper;

    private MachineService machineService;

    @Autowired
    @Lazy
    public void setMachineService(MachineService machineService) {
        this.machineService = machineService;
    }

    @Override
    public List<PartDTO> getAll(String keyword) {
        // 1.获取设备别权限
        List<MachineDTO> machineList = machineService.getAll(null);
        CollUtil.filter(machineList, machine -> {
            return machine.getMachine().getEnabled();
        });
        // 1.2 组装成 工程别 ID Set
        HashSet<Integer> secdIDHashSets = CollUtil.newHashSet();
        CollUtil.forEach(machineList.iterator(), (machine, index) -> {
            secdIDHashSets.add(machine.getSecDept().getDepartmentID());
        });
        // 2. 查询part
        LambdaQueryWrapper<PartPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PartPO::getSecDeptID, secdIDHashSets);
        List<PartPO> partPOList = partPOMapper.selectList(wrapper);
        //2.1 组装结果
        List<PartDTO> partDTOList = CollUtil.list(false);
        CollUtil.forEach(partPOList.iterator(), (part, index) -> {
            PartDTO partDTO = new PartDTO();
            partDTO.setPart(part);
            MachineDTO tmpMachineDTO = CollUtil.findOne(machineList, machineDTO -> {
                return machineDTO.getSecDept().getDepartmentID().equals(part.getSecDeptID());
            });
            partDTO.setSecDept(tmpMachineDTO.getSecDept());
            partDTOList.add(partDTO);
        });

        //3.关键字查找
        CollUtil.filter(partDTOList, partDTO -> {
            return (partDTO.getPart().getPartName().contains(keyword) || partDTO.getPart().getMachineNo().contains(keyword) || partDTO.getSecDept().getDepartmentName().contains(keyword));
        });
        return partDTOList;
    }

    @Override
    public int uptPartEnabled(Integer partID, Boolean enabled) {
        PartPO partPO = new PartPO();
        partPO.setPartID(partID);
        partPO.setEnabled(enabled);
        return partPOMapper.updateById(partPO);
    }

    @Override
    public int delPart(Integer partID) {
        return partPOMapper.deleteById(partID);
    }

    @Override
    public int addPart(PartPO partPO) {
        return partPOMapper.insert(partPO);
    }

    @Override
    public int uptPart(PartPO partPO) {
        return partPOMapper.updateById(partPO);
    }

    @Override
    public List<PartPO> getListByMachineNo(String machineNo) {
        //1.通过设备获取secDeptID
        MachinePO machinePO = machineService.get1ByMachineNo_Usable(machineNo);
        if(ObjectUtil.isEmpty(machinePO)) return null;
        //2.获取数据
        LambdaQueryWrapper<PartPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PartPO::getSecDeptID,machinePO.getSecDeptID())
                .or()
                .eq(PartPO::getMachineNo,machinePO.getMachineNo());
        return partPOMapper.selectList(wrapper);
    }
}
